package pl.spikes;

import pl.spikes.R;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;


public class AsynctaskSpikeActivity extends Activity implements OnClickListener {
 
 ProgressBar progressBar;
 Button buttonStartProgress;
 TextView message;
 TextView counter;
 String count;
 
 public class BackgroundAsyncTask extends
    AsyncTask<Void, Integer, Void> {
  
  int myProgress;

  @Override
  protected void onPostExecute(Void result) {
   // TODO Auto-generated method stub
   Toast.makeText(AsynctaskSpikeActivity.this,
         "onPostExecute", Toast.LENGTH_LONG).show();
   buttonStartProgress.setClickable(true);
  }

  @Override
  protected void onPreExecute() {
   // TODO Auto-generated method stub
   Toast.makeText(AsynctaskSpikeActivity.this,
         "onPreExecute", Toast.LENGTH_LONG).show();
   myProgress = 0;
   count="";
  }

  //The value of myProgress will be the input to the onProgressUpdate()
  @Override
  protected Void doInBackground(Void... params) {
   while(myProgress<100){
    myProgress++;
    publishProgress(myProgress);
    count="Value of counter"+myProgress;  // just for displaying the counter
       SystemClock.sleep(100); // sleeps for 100 milliseconds
   }
   return null;
  }

  
  @Override
  protected void onProgressUpdate(Integer... values) {
   // TODO Auto-generated method stub
   progressBar.setProgress(values[0]); 
   counter.setText(count);  
  }

 }
 
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);  
     buttonStartProgress = (Button)findViewById(R.id.start_counter_btn);
     View displayMessageBtn = findViewById(R.id.display_message_btn);
     View displayMessage2Btn = findViewById(R.id.display_message2_btn);
     View nextScreenBtn = findViewById(R.id.next_screen_btn);
     message = (TextView) findViewById(R.id.message_tv);
     counter = (TextView) findViewById(R.id.value_of_counter_tv);
     displayMessageBtn.setOnClickListener(this);
     displayMessage2Btn.setOnClickListener(this);
     nextScreenBtn.setOnClickListener(this);
     buttonStartProgress.setOnClickListener(this);
     progressBar = (ProgressBar)findViewById(R.id.progressbar_Horizontal);
     progressBar.setProgress(0); 
     
     
   }
 
@Override
public void onClick(View v) {
 // TODO Auto-generated method stub
	   switch(v.getId()) {
		
		case R.id.start_counter_btn:
			new BackgroundAsyncTask().execute();
		    buttonStartProgress.setClickable(false);			
			break;
		case R.id.display_message_btn:
			message.setText("Since you clicked the Display Message 1 button, " +
					"you will see this message");
			break;
		case R.id.display_message2_btn:
			message.setText("This message is activated because you have clicked Display Message 2 button");
			break;
		case R.id.next_screen_btn:
			// go to next screen
			Intent next_screen_intent = new Intent(this, JsonSpikeActivity.class);
			startActivity(next_screen_intent);
		default:	
			message.setText("This is a default message");
		} 
	   
}
   
}